#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<time.h>
#define MAXLINE 4096


bool file_exist(char *filename) {
    bool exist = access(filename, F_OK) == 0;
    // printf("%s file_exist:%d, err:%s\n", filename, access(filename, F_OK), strerror(errno));
    return exist;
}

int main(int argc, char** argv){
    int  listenfd, connfd;
    struct sockaddr_in  servaddr;
    char  buff[4096];
    FILE *fp;
    int  n;

    if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
        printf("server create socket error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(6666);
    //设置端口可重用
    int contain;
    setsockopt(listenfd,SOL_SOCKET, SO_REUSEADDR, &contain, sizeof(int));

    if( bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
        printf("server bind socket error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }

    if( listen(listenfd, 10) == -1){
        printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
        return 0;
    }
    printf("\nserver wait client...\n");
    while(1) {
        struct sockaddr_in client_addr;
        socklen_t size=sizeof(client_addr);
        if( (connfd = accept(listenfd, (struct sockaddr*)&client_addr, &size)) == -1){
            printf("server accept socket error: %s(errno: %d)",strerror(errno),errno);
            continue;
        }
        int ip = client_addr.sin_addr.s_addr >> 24 & 0xff;
        time_t now_time=time(NULL);  
        tm*  t_tm = localtime(&now_time);
        char save_file[512];
        char path[1024];
        int index = 0;
        do {
            sprintf(save_file, "%03d@%02d-%02d-%02d-%02d-%02d-%02d.png", ip, t_tm->tm_mon, t_tm->tm_mday, t_tm->tm_hour, t_tm->tm_min, t_tm->tm_sec, index);
            sprintf(path, "/home/yangtingbin/pic/%s", save_file);
            index++;
        } while(file_exist(path));
        printf("server save_file:%s\n", path);
        if((fp = fopen(path, "ab") ) == NULL) {
            printf("server open file failed:%s\n", strerror(errno));
            close(listenfd);
            exit(1);
        }
        // usleep(5 * 1000 * 1000);
        while(1){
            n = read(connfd, buff, MAXLINE);
            if(n == 0)
                break;
            fwrite(buff, 1, n, fp);
        }
        buff[n] = '\0';
        printf("server recv msg from client:%d\n", ip);
        if(connfd) {
            close(connfd);
        } else {
            printf("server already close connfd:%d\n", connfd);
        }
        if(fp) {
            fclose(fp);
        } else {
            printf("server already close fp:%p\n", fp);
        }
        // printf("server fp:%p, connfd:%d\n", fp, connfd);
    }
    close(listenfd);
    return 0;
}

